#include <iostream>
#include <map>
#include <algorithm> //std::for_each
#include <functional>   //std::unary_function
using namespace std;

//THe populateContainer() function is identical to the one shown earlier
//for comparison algoritms,so it is omitted here.

//Function template to populate a container of ints.(用于填充整数容器的函数模板。)
//The container must support push_back().
template<typename Container>
void populateContainer(Container& cont)
{
    int num;
    while(true){
        cout << "Enter a number(0 to quit): ";
        cin >> num;
        if(num == 0){
            break;
        }
        cont.push_back(num);
    }
}
class SumAndProd :public unary_function<int,void>
{
    public:
        SumAndProd() :sum(0),prod(1){}
        void operator()(int elem);
        int sum;
        int prod;
};
void SumAndProd::operator()(int elem)
{
    sum += elem;
    prod *= elem;
}

int main()
{
    vector<int> myVector;
    populateContainer(myVector);
    SumAndProd func;
    func =for_each(myVector.cbegin(),myVector.cend(),func);
    cout << "The sum is " << func.sum << endl;
    cout << "The product is " << func.prod << endl;
    return 0;
}